from selenium import webdriver
from time import sleep
from bs4 import BeautifulSoup as bs

url = 'https://s.weibo.com/weibo/%E5%B0%8F%E7%8C%AB%E5%92%AA%E5%A4%B4%E5%83%8F&timescope=custom:2020-11-01-09:2020-11-19-09&refer=g' #打开一个商品的评论信息链接
browser = webdriver.Edge() #打开浏览器
browser.get(url) #获取网页信息
sleep(5)

html = browser.page_source #获取源代码
soup = bs(html,'html')
pics = soup.find_all('ul',class_="m3")

'''优化代码'''
picture_all = []
for i in range(len(pics)):
    p = list(pics[i])
    L = []
    for i in range(len(p)):
        l = p[i] 
        if l!=' ':
            L.append(l)
 
    for i in range(len(L)):
        s = list(L[i])[0]["src"]
        picture_all.append(s)

'''
#p1 = list(pics)  #所有图片信息
#p2 = list(pics[0])  #取第一个微博的8张图片信息，这里的0可为pics的长度
#print(p2) #在列表p2中有1-14个元素，6个是空的
' 第一个微博的图片 '
p1 = list(pics[0])
L1 = [] #L是删除空格后的p1
for i in range(len(p1)):
    l1 = p1[i] 
    if l1!=' ':
        L1.append(l1)

picture1 = [] #第一个微博的8张图片链接
for i in range(len(L1)):
    p = list(L1[i])[0]["src"]
    picture1.append(p) 

' 第二个有图片的微博 '
p2 = list(pics[1])  
L2 = [] #删除空格后的p2
for i in range(len(p2)):
    l2 = p2[i] 
    if l2!=' ':
        L2.append(l2)
        
picture2 = [] 
for i in range(len(L2)):
    p = list(L2[i])[0]["src"]
    picture2.append(p) 

' 第三个有图片的微博 '
p3 = list(pics[2])  
L3 = [] #删除空格后的p2
for i in range(len(p3)):
    l3 = p3[i] 
    if l3!=' ':
        L3.append(l3)
        
picture3 = [] 
for i in range(len(L3)):
    p = list(L3[i])[0]["src"]
    picture3.append(p) 

' 第四个有图片的微博 '
p4 = list(pics[3])  
L4 = [] #删除空格后的p2
for i in range(len(p4)):
    l4 = p4[i] 
    if l4!=' ':
        L4.append(l4)
        
picture4 = [] 
for i in range(len(L4)):
    p = list(L4[i])[0]["src"]
    picture4.append(p) 

' 第五个有图片的微博 '
p5 = list(pics[4])  
L5 = [] #删除空格后的p2
for i in range(len(p5)):
    l5 = p5[i] 
    if l5!=' ':
        L5.append(l5)
        
picture5 = [] 
for i in range(len(L5)):
    p = list(L5[i])[0]["src"]
    picture5.append(p) 

picture_all = picture1+picture2+picture3+picture4+picture5
'''

